package com.dm0858.bianmin.ui.activity;

import android.content.Intent;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.dm0858.bianmin.R;
import com.dm0858.bianmin.model.response.ArticleDetailResponse;
import com.dm0858.bianmin.ui.base.BaseActivity;
import com.dm0858.bianmin.ui.base.BasePresenter;
import com.dm0858.bianmin.ui.presenter.ArticleDetailPresenter;
import com.dm0858.bianmin.ui.presenter.ShangJiaPresenter;
import com.dm0858.bianmin.utils.DateUtils;
import com.dm0858.bianmin.view.IArticleDetailView;
import com.dm0858.bianmin.view.IShangJiaView;
import com.socks.library.KLog;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import butterknife.Bind;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import static com.dm0858.bianmin.app.ApiConstant.BASE_SERVER_URL;

/**
 * Created by yelvqing on 2018/4/30.
 */

public class ArticleDetailActivity extends BaseActivity<ArticleDetailPresenter> implements IArticleDetailView {

    public static final String id = "article1";
    protected String mid;

    @Bind(R.id.iv_back)
    ImageView mIvBack;

    @Bind(R.id.webView)
    WebView mcontent;
    @Bind(R.id.normal_title)
    TextView mtitle2;

    @Bind(R.id.news_time)
    TextView mnews_time;

    @Override
    protected ArticleDetailPresenter createPresenter() {
        return new ArticleDetailPresenter(this);
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_article_detail;
    }

    @Override
    public void onGetArticleDetail(ArticleDetailResponse newList) {


        mtitle2.setText(newList.data.title);
        String timestr = DateUtils.dataOne(newList.data.addtime);
        mnews_time.setText(DateUtils.timedate(timestr));

        WebSettings webSettings = mcontent.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setAllowFileAccess(true);
        webSettings.setSupportMultipleWindows(true);
        webSettings.setJavaScriptEnabled(true);

        webSettings.setDomStorageEnabled(true);





        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        String baseUrl = "http://bmshfw.net/";
        String css = "<style type=\"text/css\"> img {" +
                "width:100%;" +
                "height:auto;" +
                "}" +

                "</style>";
        String html = "<html><header>" + css + "</header><body>" + newList.data.content.toString() + "</body></html>";

        mcontent.loadDataWithBaseURL(baseUrl, html, "text/html" , "utf-8", null);
    }

    @Override
    public void onError() {

    }


    @Override
    public void initView() {



    }

    @Override
    public void initData() {

        Intent intent = getIntent();

        mid = intent.getStringExtra(id);




        mPresenter.getArticleDetail(mid);


    }

    @Override
    public void initListener() {






    }

    @OnClick(R.id.iv_back)
    public void onViewClicked() {
        finish();
    }
}
